//
//  HLIntimacyListViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/6/16.
//

#import "HLIntimacyListViewController.h"
#import "HLUserDetailViewController.h"
#import "HLIntimacyListRequest.h"
#import "HLIntimacyListCell.h"

@interface HLIntimacyListViewController ()

@end

@implementation HLIntimacyListViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.request = [[HLIntimacyListRequest alloc] init];
    self.request.delegate = self;
    [self requestInit];
}

- (void)requestInit
{
    [self showEmptyView];
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.request start];
}
- (void)initTableView
{
    [super initTableView];
    [self setupMJRefresh];
    [self.tableView registerClass:[HLIntimacyListCell class] forCellReuseIdentifier:[HLIntimacyListCell identifyCell]];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasources.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLIntimacyListCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLIntimacyListCell identifyCell]];
    if (self.datasources.count <= indexPath.row){
        return cell;
    }
    cell.model = self.datasources[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLIntimacyModel *record = self.datasources[indexPath.row];
    HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initWithUserId:record.userId];
    [self.navigationController pushViewController:userDetailVC animated:YES];
}


- (void)headerRefresh
{
    HLIntimacyListRequest *intimacyRequest = (HLIntimacyListRequest *)self.request;
    intimacyRequest.page = 1;
    [self.request start];
}

- (void)footerRefresh
{
    HLIntimacyListRequest *intimacyRequest = (HLIntimacyListRequest *)self.request;
    intimacyRequest.page += 1;
    [self.request start];
}


- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    
    HLIntimacyListRequest *intimacyRequest = (HLIntimacyListRequest *)request;
    if (intimacyRequest.page == 1){
        [self.datasources removeAllObjects];
    }
    [self.datasources addObjectsFromArray:intimacyRequest.intimacyList];
    if (intimacyRequest.intimacyList.count == 0){
        [self.emptyView setLoadingViewHidden:YES];
        [self.emptyView setImage:UIImageMake(@"empty_icon_2")];
        [self.emptyView setTextLabelText:@"空空如也"];
    } else {
        [self hideEmptyView];
    }
    
    [self.tableView reloadData];
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [super requestFailed:request];
}

#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}

@end
